package com.yango.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.yango.commonutil.result.Result;
import com.yango.vod.service.VodService;
import com.yango.vod.utils.AliyunVodSDKUtil;
import com.yango.vod.utils.ReadPropertiesUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author Li.Ding.yin
 * @create 2021年07月29日11:38-星期四
 * @role 阿里云视频点播微服务
 */
@Api(description = "阿里云视频点播微服务")
@RestController
@RequestMapping("/vod/video")
public class VodController {

    //注入service
    @Autowired
    private VodService vodService;

    //上传视频到阿里云-返回视频id
    @ApiOperation(value = "上传视频到阿里云-返回视频id")
    @PostMapping("/upload")
    public Result upload(@ApiParam(name = "file", value = "视频资源", required = true)
                         @RequestParam("file") MultipartFile file) {
        String videoId = vodService.uploadVideo(file);
        if (videoId != null) {
            return Result.ok().message("视频上传成功!").data("videoId", videoId);
        } else {
            return Result.error().message("视频上传失败!");
        }
    }

    //删除云端视频-单个视频删除
    @ApiOperation(value = "删除云端视频")
    @DeleteMapping("/removeVideo/{videoId}")
    public Result removeVideo(@ApiParam(name = "videoId", value = "视频id", required = true)
                              @PathVariable String videoId) {
        //调用方法删除视频
        boolean flag = vodService.removeVideo(videoId);
        if (flag) {
            return Result.ok().message("删除视频成功!");
        } else {
            return Result.error().message("删除视频失败!");
        }
    }

    //根据id批量删除视频
    @ApiOperation(value = "根据id批量删除视频")
    @DeleteMapping("/removeVideoByIds")
    public Result removeVideoByIds(@ApiParam(name = "videoIdList", value = "视频id集合", required = true)
                                   @RequestParam List videoIdList) {
        //调用方法删除
        boolean flag = vodService.removeVideoByIds(videoIdList);
        if (flag) {
            return Result.ok().message("视频删除成功!");
        } else {
            return Result.error().message("视频删除失败!");
        }
    }

    //获取视频播放地址
    @ApiOperation(value = "获取视频播放地址")
    @GetMapping("/getPlayAuth/{videoId}")
    public Result getPlayAuth(@ApiParam(name = "videoId", value = "视频id", required = true)
                              @PathVariable String videoId) throws Exception {
        //获取阿里云存储相关常量
        String accessKeyId = ReadPropertiesUtil.ACCESS_KEY_ID;
        String accessKeySecret = ReadPropertiesUtil.ACCESS_KEY_SECRET;
        //初始化
        DefaultAcsClient client = AliyunVodSDKUtil.initVodClient(accessKeyId, accessKeySecret);
        //请求
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        //设置视频id
        request.setVideoId(videoId);
        //响应
        GetVideoPlayAuthResponse response = client.getAcsResponse(request);
        //获取播放凭证
        String playAuth = response.getPlayAuth();
        if (StringUtils.isEmpty(playAuth))
            return Result.error().message("获取视频播放凭证失败!");
        return Result.ok().data("playAuth", playAuth).message("获取播放凭证成功!");
    }

}
